<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>接口调用工具</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
		<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
		<link rel="stylesheet" href="./css/font.css">
		<link rel="stylesheet" href="./css/xadmin.css">
		<script type="text/javascript" src="js/jquery.min.js"></script>
		<script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
		<script type="text/javascript" src="./js/xadmin.js"></script>
		<script src="js/config.js" type="application/javascript"></script>
		<!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
		<!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
	</head>

	<body>
		<div class="x-body">
			<form class="layui-form" lay-filter="apiinfo">
				<div class="layui-form-item">
					<label class="layui-form-label">选择URL</label>
					<div class="layui-input-block">
						<select id="url" name="url" id="url" lay-filter="url" lay-search class="selector">
						</select>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">输入URL</label>
					<div class="layui-input-block">
						<input type="text" onchange="delurlselect()" name="url2" id="url2" autocomplete="off" placeholder="输入URL" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">请求方式</label>
					<div class="layui-input-block">
						<input type="radio" name="method" lay-filter='method' value="post" title="POST请求" checked>
						<input type="radio" name="method" lay-filter='method' value="get" title="GET请求">
					</div>
				</div>
				<div class="layui-form-item" id="contenttypelist">
					<label class="layui-form-label">ContentType</label>
					<div class="layui-input-block">
						<input type="radio" name="contenttype" lay-filter='contenttype' value="json" title="application/json;charset=utf-8" checked>
						<input type="radio" name="contenttype" lay-filter='contenttype' value="form" title="application/x-www-form-urlencoded; charset=utf-8">
						<input type="radio" name="contenttype" lay-filter='contenttype' value="text" title="text/html">
					</div>
				</div>
				<div class="layui-form-item layui-form-text" id="body">
					<label class="layui-form-label">BODY</label>
					<div class="layui-input-block">
						<textarea name="body" placeholder="请输入内容" class="layui-textarea"></textarea>
					</div>
				</div>

				<div id="params" style="padding-bottom: 20px;">
					<div id="param1">
						<div class="layui-inline">
							<label class="layui-form-label">Key: </label>
							<div class="layui-input-inline">
								<input type="text" class="layui-input" name="key1" id="key1" value='' />
							</div>
						</div>
						<div class="layui-inline" style="margin-left: -50px;">
							<label class="layui-form-label"> Value: </label>
							<div class="layui-input-inline">
								<input class="layui-input" type="text" name="value1" id="value1" value='' />
							</div>
							<div class="layui-input-inline">
								<input type="button" class="layui-btn layui-btn-normal" name="del" value="删除" onclick="delRow(1)" style="background-color: #e62727; color: #fff;" onclick="delRow(1)">
							</div>
						</div>
					</div>
				</div>

				<div class="layui-form-item">
					<div class="layui-input-block">
						<button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
						<input type="button" class="layui-btn layui-btn-primary" onclick="fresh()" value="重置"></input>
						<button class="layui-btn"  lay-submit lay-filter="save" class="layui-btn layui-btn-primary" style="background: #1E9FFF;">保存接口</button>
						<button class="layui-btn"  lay-submit lay-filter="del" class="layui-btn layui-btn-primary" style="background: crimson;">删除接口</button>
						<input type="button" class="layui-btn layui-btn-primary" id="addparam" onclick="addRow()" value="添加参数"></input>
					</div>
				</div>

			</form>
		</div>
		<script>
			var url,contenttype,method,body;
			layui.use(['layer', 'form'], function() {
				var layer = layui.layer;
				var form = layui.form;
				$("#params").hide();
				$("#addparam").hide();
				$.ajax({ //异步请求返回给后台 
					url: base_url + '/test/list',
					contentType: 'application/json; charset=utf-8',
					data: '',
					type: 'get',
					dataType: 'json',
					async: false,
					success: function(result) {
						if(result.code == '0') {
							var select = document.getElementById("url")
							select.options.add(new Option("",""));
							$.each(result.data, function(n, value) {
								select.options.add(new Option(value.url, value.url));
							});
							form.render('select');
						} else {
							layer.msg(result.msg);
						}
					},
				});
				
				//监听提交
				form.on('submit(formDemo)', function(data) {
					$.ajax({
						url: base_url + '/test/api',
						type: 'post',
						contentType: 'application/json; charset=utf-8',
						data: JSON.stringify(data.field),
						dataType: 'text',
						async: false,
						success: function(result) {
							if(result == "地址不能为空") {
								layer.msg("地址不能为空");
								return;
							}
							if(result == "地址错误") {
								layer.msg("地址错误");
								return;
							}
							parent.layer.open({
								type: 1,
								title: '执行结果',
								area: ['1000px', '400px'],
								maxmin: true,
								content: "<div style='padding:10px'><textarea class=\"layui-textarea\" style='height:330px'>"+result+"</textarea></div>"
							});
						}
					});
					return false;
				});
				
				form.on('submit(save)', function(data) {
					$.ajax({
						url: base_url + '/test/save',
						type: 'post',
						contentType: 'application/json; charset=utf-8',
						data: JSON.stringify(data.field),
						dataType: 'text',
						async: false,
						success: function(result) {
							if(result == "地址不能为空") {
								layer.msg("地址不能为空");
								return;
							}
							if(result == "地址错误") {
								layer.msg("地址错误");
								return;
							}
							layer.msg("保存成功")
						}
					});
					return false;
				});
				
				form.on('submit(del)', function(data) {
					layer.confirm('确定要删除吗？', function(index) {
						$.ajax({
							url: base_url + '/test/del',
							type: 'post',
							contentType: 'application/json; charset=utf-8',
							data: JSON.stringify(data.field),
							dataType: 'text',
							async: false,
							success: function(result) {
								if(result == "地址不能为空") {
									layer.msg("地址不能为空");
									return;
								}
								if(result == "地址错误") {
									layer.msg("地址错误");
									return;
								}
								layer.msg("删除成功");
								return;
							}
						});
						
					});
					return false;
				});
				
				form.on('select(url)', function(data) {
					url = data.value;
					if(url == null || url == "") {
						return;
					}
					form.val('apiinfo', {
						"url2": ""
					});
					$.ajax({
						url: base_url + '/test/get',
						type: 'post',
						contentType: 'application/json; charset=utf-8',
						data: "{\"url\":\""+url+"\"}",
						dataType: 'json',
						async: false,
						success: function(result) {
							if(result.code == '0') {
								contenttype = result.data[0].contenttype;
								method = result.data[0].method;
								body = result.data[0].body;
								form.val('apiinfo', {
									"method": method
								});
								if(method == "get") {
									$("#contenttypelist").hide();
									$("#params").hide();
									$("#addparam").hide();
									$("#body").hide();
								}else{
									$("#contenttypelist").show();
									form.val('apiinfo', {
										"contenttype": contenttype
									});
									if(contenttype == 'form') {
										$("#params").show();
										$("#params").empty();
										$("#body").hide();
										var keyvalue = body.split("&");
										for(var i = 0; i < keyvalue.length; i++) {
											$("#params").append("					<div id=\"param"+(i+1)+"\" style='padding-top:5px'>						<div class=\"layui-inline\">							<label class=\"layui-form-label\">Key: </label>							<div class=\"layui-input-inline\">								<input type=\"text\" class=\"layui-input\" name=\"key"+(i+1)+"\" id=\"key"+(i+1)+"\" value='"+keyvalue[i].split("=")[0]+"' />							</div>						</div>						<div class=\"layui-inline\" style=\"margin-left: -50px;\">							<label class=\"layui-form-label\"> Value: </label>							<div class=\"layui-input-inline\">								<input class=\"layui-input\" type=\"text\" name=\"value"+(i+1)+"\" id=\"value"+(i+1)+"\" value='"+keyvalue[i].split("=")[1]+"' />							</div>							<div class=\"layui-input-inline\">								<input type=\"button\" class=\"layui-btn layui-btn-normal\" name=\"del\" value=\"删除\" style=\"background-color: #e62727; color: #fff;\" onclick=\"delRow("+(i+1)+")\">							</div>						</div>					</div>")
										}
									}else{
										$("#params").hide();
										$("#body").show();
										form.val('apiinfo', {
											"body": body
										});
									}
								}
							} else {
								layer.msg(result.msg);
							}
						}
					});
				});

				form.on('radio(method)', function(data) {
					if(data.value == "get") {
						$("#body").hide();
						$("#contenttypelist").hide();
						for(var i = 1; i < 100; i++) {
							delRow(i);
						}
					} else {
						if($("input[name='contenttype']:checked").val() != 'form') {
							$("#body").show();
						}
						$("#contenttypelist").show();
					}

				});

				form.on('radio(contenttype)', function(data) {
					if(data.value == "form") {
						$("#body").hide();
						$("#params").show();
						$("#addparam").show();
					} else {
						$("#body").show();
						$("#params").hide();
						$("#addparam").hide();
					}
				});
				
			});


			//添加新的参数输入行
			function addRow() {
				for(var i = 1; i < 100; i++) {
					if($("#param" + i).html() == null) {
						$("#params").append("					<div id=\"param"+i+"\" style='padding-top:5px'>						<div class=\"layui-inline\">							<label class=\"layui-form-label\">Key: </label>							<div class=\"layui-input-inline\">								<input type=\"text\" class=\"layui-input\" name=\"key"+i+"\" id=\"key"+i+"\" value='' />							</div>						</div>						<div class=\"layui-inline\" style=\"margin-left: -50px;\">							<label class=\"layui-form-label\"> Value: </label>							<div class=\"layui-input-inline\">								<input class=\"layui-input\" type=\"text\" name=\"value"+i+"\" id=\"value"+i+"\" value='' />							</div>							<div class=\"layui-input-inline\">								<input type=\"button\" class=\"layui-btn layui-btn-normal\" name=\"del\" value=\"删除\" style=\"background-color: #e62727; color: #fff;\" onclick=\"delRow("+i+")\">							</div>						</div>					</div>")
						return;
					}
				}
				myallert("超过最大限制");
			}

			//删除当前行
			function delRow(index) {
				if($("#param" + index).html() != null) {
					$('#param' + index).remove();
				}
				return;
			}
			
			function delurlselect() {
				layui.use(['form'], function() {
					var form = layui.form;
					form.val('apiinfo', {
						"url": ""
					});
				});
			}
			
		</script>
	</body>
</html>